package com.starbaba.system.userofrole.dao;

import java.util.List;

import com.starbaba.core.hibernate.DAOFactory;
import com.starbaba.core.hibernate.HibernateDAO;
import com.starbaba.core.hibernate.Strategy;
import com.starbaba.core.web.constant.WebConstants;
import com.starbaba.system.managelog.model.ManagelogVO;
import com.starbaba.system.managelog.model.OperAction;
import com.starbaba.system.managelog.model.OperState;
import com.starbaba.system.user.model.UserVO;
import com.starbaba.system.userofrole.model.UserOfRoleVO;

@SuppressWarnings("serial")
public class UserOfRoleStrategy implements Strategy {

	private UserOfRoleVO urVO;

	private UserVO userVO;

	private String oprType = WebConstants.OPRTYPE_ADD;


	public UserOfRoleStrategy(UserOfRoleVO urVO, UserVO userVO) {
		super();
		this.urVO = urVO;
		this.userVO = userVO;
		
	}

	@SuppressWarnings("unused")
    @Override
	public Object process() throws Exception {
		HibernateDAO urDAO = DAOFactory.getInstance().createHibernateDAO(
				UserOfRoleVO.class);
		//HibernateDAO logDAO = DAOFactory.getInstance().createHibernateDAO(ManagelogVO.class);
		if (WebConstants.OPRTYPE_ADD.equals(oprType)) {
			List<?> list = urDAO.findByHQL(
					" from UserOfRoleVO where userid = ? and roleid = ?  ",
					new Object[] { urVO.getUserid(), urVO.getRoleid()});
			if (null != list && list.size() > 0) {
				throw new Exception("用户角色关系已经存在");
			}
			// 保存新增操作日志
			ManagelogVO logVO = new ManagelogVO().createManagelogVO(
					userVO.getUserid(), "userofrole", OperAction.INSERT,
					urVO, null, OperState.SUCCESS);
		    //logDAO.save(logVO);
			urDAO.save(urVO);
		} else if (WebConstants.OPRTYPE_MODIFY.equals(oprType)) {
			urDAO.saveOrUpdate(urVO);
		} else if (WebConstants.OPRTYPE_DELETE.equals(oprType)) {
			urDAO.deleteByVO(urVO);
		}
		return null;
	}

	public UserOfRoleVO getUrVO() {
		return urVO;
	}

	public void setUrVO(UserOfRoleVO urVO) {
		this.urVO = urVO;
	}

	public String getOprType() {
		return oprType;
	}

	public void setOprType(String oprType) {
		this.oprType = oprType;
	}

	/**
	 * @return userVO
	 */
	
	public UserVO getUserVO() {
		return userVO;
	}

	/**
	 * @param userVO the userVO to set
	 */
	public void setUserVO(UserVO userVO) {
		this.userVO = userVO;
	}

}
